﻿var TimerID;
var IsAuto = false;

$(document).ready(function () {

    $("select[name$='LevelList']").css("width", "98%").css("padding-left", "2%");
    $("#EntryPoint").css("position", "absolute").css("top", "-100px");
    //    //$("#FirstLevel").css("height", "200px")//($(document).height() - 20) / 3 + " px");
    //    // $("#SecondLevel").css("height", ($(document).height() - 80) + " px");
    //    //  $("#FirstLevelList").css("height", "90%");
    //    //$("#SecondLevelList").css("height", "75%");

    if ($("#txtSpeed").length) $("#txtSpeed")[0].value = GetTimeBetween()
    if ($("#txtSpeed1").length) $("#txtSpeed1")[0].value = GetTimeBetween1()

    ColorLamp("")
    ColorLamp1("")

    $(".pushbutton,#imgbtnFilterPortfolios,#lblRefresh").click(function () {
        $(this).css("position", "relative");
        $(this).css("left", "1px");
        $(this).css("top", "1px");
    });

    $("#EntryPointSelect").change(function () {
        //        //stop the slide show
        //        setCookie("Direction", 0, 1)
        //        setCookie("LampColor", sColor, 1)
        //        if (TimerID != null) clearInterval(TimerID)
        //        evt.preventDefault()
        //        ColorLamp("blue")
        //debugger

    }
       );
    if ($("#EntryPointSelect option:selected")[0].value == "Report") {
        $("#pnlSlideShow").css("display", "none")
        
    }

    $("#printer", parent.frames[0].document.forms[0]).css("display", ($("#EntryPointSelect option:selected")[0].value == "Report") ? "none" : "" )

    $("#FirstLevelList").change(function () {
        var nDirection = parseInt(getCookie("Direction"))
        if (Math.abs(nDirection) != 1) {
            $(this).hide("slow");
            $("#imgLoadingFirst").css("display", "");
        }

        if ($("#txtSpeed1").length) $("#txtSpeed1")[0].value = GetTimeBetween1()

        var CurrTopLevel;
        $("#EntryPointSelect option:selected").each(function () { CurrTopLevel = $(this)[0].value; });
        if (CurrTopLevel != "Report") {
            //            if (this.selectedIndex > 0)
            //                parent.reports.location = this.options[this.selectedIndex].value
            //        } else {

            if (this.selectedIndex > 0)
                parent.reports.location =
            "reports/RollupReports.aspx?ROLLUP_TYPE="
                                + encodeURIComponent(CurrTopLevel) + "&ROLLUP_ID="
                                + encodeURIComponent(jQuery.trim(this.options[this.selectedIndex].value))
                                + "&Title=" + encodeURIComponent(this.options[this.selectedIndex].text)
        }
    });


    $("#SecondLevelList").change(function () {
        var nDirection = parseInt(getCookie("Direction"))
        if (Math.abs(nDirection) != 1) {
            $(this).hide("slow");
            $("#imgLoadingSecond").css("display", "");
        }
        if (this.selectedIndex > 0)
            parent.reports.location = "Reports/TracReports.aspx?TRAC_ID="
                    + encodeURIComponent(this.options[this.selectedIndex].value)
                    + "&Title=" + encodeURIComponent(this.options[this.selectedIndex].text)
                    + "&Units=" + $("input[name='Units']:checked").val();

    });



    $("#imgbtnRefresh1").click(function () {
        //$(this).hide("slow");

        $("#imgLoadingFirst").css("display", "");


        parent.reports.location = "Reports/RollupReports.aspx?ROLLUP_TYPE="
        + encodeURIComponent($("#EntryPointSelect option:selected")[0].value) + "&ROLLUP_ID="
        + encodeURIComponent(jQuery.trim($("#FirstLevelList option:selected")[0].value))
        + "&Title=" + encodeURIComponent($("#FirstLevelList option:selected")[0].text)
        + "&UNITS=" + $("input[name='Units']:checked").val();
    }

    );

    $("#imgbtnRefresh2").click(function () {
        //$(this).hide("slow");
        $("#imgLoadingSecond").css("display", "");
        parent.reports.location = "Reports/TracReports.aspx?TRAC_ID="
        + encodeURIComponent($("#SecondLevelList option:selected")[0].value)
        + "&Title=" + encodeURIComponent($("#SecondLevelList option:selected")[0].text)
        + "&Units=" + $("input[name='Units']:checked").val();
        if ($("#txtSpeed1").length) $("#txtSpeed1")[0].value = (getCookie1("TimeBetween") == "NaN") ? 10 : getCookie1("TimeBetween")
    }

    );

    $("#imgbtnShow3").click(function (evt) {
        evt.preventDefault()
        $("#ThirdLevel").css("display", $("#ThirdLevel")[0].style.display == "none" ? "" : "none");
        $("#FourthLevel").css("display", "none");
    });


    TimerId = setInterval(function () {
        ClickNext()
    }, GetTimeBetween() * 1000);


    //var "#FirstLevelList" = "#SecondLevelList"
    $("#lnkbtnNext").click(function (evt) {

        var txt = $("#FirstLevelList" + " :selected").text()

        if ($("#FirstLevelList")[0].selectedIndex + 1 < $("#FirstLevelList")[0].length) {
            do {
                $("#FirstLevelList")[0].selectedIndex += 1
            }
            while (txt == $("#FirstLevelList" + " :selected").text())
        }
        else
            $("#FirstLevelList")[0].selectedIndex = 1

        $("#FirstLevelList").change()
        evt.preventDefault()
    });

    $("#lnkbtnPrev").click(function (evt) {

        var txt = $("#FirstLevelList" + " :selected").text()

        if ($("#FirstLevelList")[0].selectedIndex - 1 > 0) {
            $("#FirstLevelList")[0].selectedIndex -= 1
            var txtNext = $("#FirstLevelList" + " :selected").text()
            if (txt == txtNext)
                if ($("#FirstLevelList")[0].selectedIndex - 2 > 0)
                    $("#FirstLevelList")[0].selectedIndex -= 2

            }
            else
                $("#FirstLevelList")[0].selectedIndex = $("#FirstLevelList")[0].length - 1

            $("#FirstLevelList").change()
            evt.preventDefault()
        });

        $("#lnkbtnPause").click(function (evt) {
            //setCookie("Direction", 0, 1)
            if (TimerID != null) clearInterval(TimerID)
            ColorLamp("yellow")
            evt.preventDefault()
        });


        $("#lnkbtnForward").click(function (evt) {

            ColorLamp("green")
            setCookie("Direction", 1, 1)
            TimerId = setInterval(function () {
                ClickNext()
            }, GetTimeBetween() * 1000)
            evt.preventDefault()
        });

        $("#lnkbtnBack").click(function (evt) {
            setCookie("Direction", -1, 1)
            ColorLamp("green")
            TimerId = setInterval(function () {
                ClickNext()
            }, GetTimeBetween() * 1000)
            evt.preventDefault()
        });

        $("#lnkbtnLast").click(function (evt) {
            ColorLamp("blue")
            setCookie("Direction", 0, 1)
            $("#FirstLevelList")[0].selectedIndex = $("#FirstLevelList")[0].length - 1
            $("#FirstLevelList").change()
            evt.preventDefault()
        });

        $("#lnkbtnStop").click(function (evt) {
            //setCookie("Direction", 0, 1)
            if (TimerID != null) clearInterval(TimerID)
            ColorLamp("red")
            evt.preventDefault()
        });

        $("#lnkbtnSlower").click(function (evt) {
            var nMillisecs = parseInt(GetTimeBetween() * 1000)
            if (nMillisecs < 60000) nMillisecs += 1000
            setCookie("TimeBetween", nMillisecs / 1000, 30)
            clearInterval(TimerId)
            TimerId = setInterval(function () {
                ClickNext()
            }, GetTimeBetween() * 1000);

            evt.preventDefault()
            $("#txtSpeed")[0].value = parseInt(getCookie("TimeBetween"))
        });

        $("#lnkbtnFaster").click(function (evt) {
            var nMillisecs = parseInt(GetTimeBetween() * 1000)
            if (nMillisecs > 1000) nMillisecs -= 1000
            setCookie("TimeBetween", nMillisecs / 1000, 30)
            clearInterval(TimerId)
            TimerId = setInterval(function () {
                ClickNext()
            }, GetTimeBetween() * 1000);
            evt.preventDefault()
            $("#txtSpeed")[0].value = parseInt(getCookie("TimeBetween"))
        });

        $("#lnkbtnFirst").click(function (evt) {
            ColorLamp("blue")
            $("#FirstLevelList")[0].selectedIndex = 1
            $("#FirstLevelList").change()
            evt.preventDefault()
        });

        //------------------------
        TimerId1 = setInterval(function () {
            ClickNext1()
        }, GetTimeBetween1() * 1000);


        //var "#FirstLevelList" = "#SecondLevelList"
        $("#lnkbtnNext1").click(function (evt) {

            var txt = $("#SecondLevelList" + " :selected").text()

            if ($("#SecondLevelList")[0].selectedIndex + 1 < $("#SecondLevelList")[0].length) {
                $("#SecondLevelList")[0].selectedIndex += 1
                var txtNext = $("#SecondLevelList" + " :selected").text()
                if (txt == txtNext)
                    if ($("#SecondLevelList")[0].selectedIndex + 2 < $("#SecondLevelList")[0].length)
                        $("#SecondLevelList")[0].selectedIndex += 2

                }
                else
                    $("#SecondLevelList")[0].selectedIndex = 1

                $("#SecondLevelList").change()
                evt.preventDefault()
            });

            $("#lnkbtnPrev1").click(function (evt) {

                var txt = $("#SecondLevelList" + " :selected").text()

                if ($("#SecondLevelList")[0].selectedIndex - 1 > 0) {
                    $("#SecondLevelList")[0].selectedIndex -= 1
                    var txtNext = $("#SecondLevelList" + " :selected").text()
                    if (txt == txtNext)
                        if ($("#SecondLevelList")[0].selectedIndex - 2 > 0)
                            $("#SecondLevelList")[0].selectedIndex -= 2

                    }
                    else
                        $("#SecondLevelList")[0].selectedIndex = $("#SecondLevelList")[0].length - 1

                    $("#SecondLevelList").change()
                    evt.preventDefault()
                });

                $("#lnkbtnPause1").click(function (evt) {
                    //setCookie1("Direction", 0, 1)
                    if (TimerId1 != null) clearInterval(TimerId1)
                    ColorLamp1("yellow")
                    evt.preventDefault()
                });


                $("#lnkbtnForward1").click(function (evt) {
                    ColorLamp1("green")
                    setCookie1("Direction", 1, 1)
                    TimerId1 = setInterval(function () {
                        ClickNext1()
                    }, GetTimeBetween() * 1000)
                    evt.preventDefault()
                });

                $("#lnkbtnBack1").click(function (evt) {
                    setCookie1("Direction", -1, 1)
                    ColorLamp1("green")
                    TimerId1 = setInterval(function () {
                        ClickNext1()
                    }, GetTimeBetween() * 1000)

                    evt.preventDefault()
                });

                $("#lnkbtnLast1").click(function (evt) {
                    ColorLamp1("blue")
                    setCookie1("Direction", 0, 1)
                    $("#SecondLevelList")[0].selectedIndex = $("#SecondLevelList")[0].length - 1
                    $("#SecondLevelList").change()
                    evt.preventDefault()
                });

                $("#lnkbtnStop1").click(function (evt) {
                    //setCookie1("Direction", 0, 1)
                    if (TimerId1 != null) clearInterval(TimerId1)
                    ColorLamp1("red")
                    evt.preventDefault()
                });

                $("#lnkbtnSlower1").click(function (evt) {
                    var nMillisecs = parseInt(GetTimeBetween1() * 1000)
                    if (nMillisecs < 60000) nMillisecs += 1000
                    setCookie1("TimeBetween", nMillisecs / 1000, 30)
                    clearInterval(TimerId1)
                    TimerId1 = setInterval(function () {
                        ClickNext1()
                    }, GetTimeBetween1() * 1000);

                    evt.preventDefault()
                    $("#txtSpeed1")[0].value = parseInt(getCookie1("TimeBetween"))
                });

                $("#lnkbtnFaster1").click(function (evt) {
                    var nMillisecs = parseInt(GetTimeBetween1() * 1000)
                    if (nMillisecs > 1000) nMillisecs -= 1000
                    setCookie1("TimeBetween", nMillisecs / 1000, 30)
                    clearInterval(TimerId1)
                    TimerId1 = setInterval(function () {
                        ClickNext1()
                    }, GetTimeBetween1() * 1000);
                    evt.preventDefault()
                    $("#txtSpeed1")[0].value = parseInt(getCookie1("TimeBetween"))
                });

                $("#lnkbtnFirst1").click(function (evt) {
                    ColorLamp1("blue")
                    $("#SecondLevelList")[0].selectedIndex = 1
                    $("#SecondLevelList").change()
                    evt.preventDefault()
                });



            });



            function GetTimeBetween() {

                var nSecs = parseInt(getCookie("TimeBetween"))
                if (!nSecs || nSecs>60 || nSecs<1) nSecs = 10

                return nSecs 
            }

            function ColorLamp(sColor) {
                if (sColor == "")
                    sColor = getCookie("LampColor");
                else
                    setCookie("LampColor", sColor, 1);
                if (sColor == "") { sColor = "blue"; setCookie("LampColor", sColor, 1) }
                x = $("#imgNowDoing")
                if (x.length > 0) {
                    var src = $("#imgNowDoing")[0].attributes["src"].nodeValue
                    var saSrc = src.split("/")
                    var n = saSrc.length - 1
                    saSrc[n] = "led-" + sColor + ".gif"
                    var newSrc = saSrc.join("/")
                    $("#imgNowDoing")[0].attributes["src"].nodeValue = newSrc
                }

            }

            function setCookie(c_name, value, expiredays) {
                var exdate = new Date();
                exdate.setDate(exdate.getDate() + expiredays);
                document.cookie = c_name + "=" + escape(value) +
((expiredays == null) ? "" : ";expires=" + exdate.toUTCString());
            }

            function getCookie(c_name) {
                if (document.cookie.length > 0) {
                    c_start = document.cookie.indexOf(c_name + "=");
                    if (c_start != -1) {
                        c_start = c_start + c_name.length + 1;
                        c_end = document.cookie.indexOf(";", c_start);
                        if (c_end == -1) c_end = document.cookie.length;
                        return unescape(document.cookie.substring(c_start, c_end));
                    }
                }
                return "";
            }

            function ClickNext() {
                var nDirection = parseInt(getCookie("Direction"))
                var sColor = getCookie("LampColor")

                if (nDirection == 1 && sColor == "green")
                    $("#lnkbtnNext").click()
                else
                    if (nDirection == -1 && sColor == "green")
                        $("#lnkbtnPrev").click()
                    else
                        if (sColor != "green") clearInterval(TimerId)

            }


            //----------------------------------------------------



            function GetTimeBetween1() {

                var nSecs = parseInt(getCookie1("TimeBetween"))
                if (!nSecs  || nSecs>60 || nSecs<1) nSecs = 10

                return nSecs
            }

            function ColorLamp1(sColor) {
                if (sColor == "")
                    sColor = getCookie1("LampColor");
                else
                    setCookie1("LampColor", sColor, 1);
                if (sColor == "") { sColor = "blue"; setCookie1("LampColor", sColor, 1) }
                x = $("#imgNowDoing1")
                if (x.length > 0) {
                    var src = $("#imgNowDoing1")[0].attributes["src"].nodeValue
                    var saSrc = src.split("/")
                    var n = saSrc.length - 1
                    saSrc[n] = "led-" + sColor + ".gif"
                    var newSrc = saSrc.join("/")
                    $("#imgNowDoing1")[0].attributes["src"].nodeValue = newSrc
                }

            }

            function setCookie1(c_name, value, expiredays) {
                c_name += "1"
                var exdate = new Date();
                exdate.setDate(exdate.getDate() + expiredays);
                document.cookie = c_name + "=" + escape(value) +
((expiredays == null) ? "" : ";expires=" + exdate.toUTCString());
            }

            function getCookie1(c_name) {
                c_name += "1"
                if (document.cookie.length > 0) {
                    c_start = document.cookie.indexOf(c_name + "=");
                    if (c_start != -1) {
                        c_start = c_start + c_name.length + 1;
                        c_end = document.cookie.indexOf(";", c_start);
                        if (c_end == -1) c_end = document.cookie.length;
                        return unescape(document.cookie.substring(c_start, c_end));
                    }
                }
                return "";
            }

            function ClickNext1() {
                var nDirection = parseInt(getCookie1("Direction"))
                var sColor = getCookie1("LampColor")

                if (nDirection == 1 && sColor == "green")
                    $("#lnkbtnNext1").click()
                else
                    if (nDirection == -1 && sColor == "green")
                        $("#lnkbtnPrev1").click()
                    else
                        if (sColor != "green") clearInterval(TimerId1)

            }

